package com.example.xm.ui.home.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.viewpager2.widget.ViewPager2;

import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.PlayVideoAdapter;
import com.example.xm.base.BaseActivity;
import com.example.xm.bean.HomeBean.HomeVideoBean;
import com.example.xm.interfaces.IBasePresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static cn.jzvd.Jzvd.CURRENT_JZVD;

public class VideoActivity extends BaseActivity {

    @BindView(R.id.viewPager2)
    ViewPager2 viewPager2;
    private List<HomeVideoBean.DataBean.ListBean> list;
    private int pos;
    private PlayVideoAdapter playVideoAdapter;

    @Override
    protected IBasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_video;
    }

    @Override
    protected void initView() {
        //监听滑动
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                playVideoAdapter.playVideo();
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
        initData();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("videos")) {
            list = ((HomeVideoBean.DataBean) intent.getSerializableExtra("videos")).getList();
            pos = intent.getIntExtra("pos", 0);
            playVideoAdapter = new PlayVideoAdapter(this, list);
            viewPager2.setAdapter(playVideoAdapter);
            if (pos > 0) {
                viewPager2.setCurrentItem(pos);
            }
        } else {

        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void showTips(String tips) {

    }

    @Override
    public void showLoading(int visble) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //关闭视频播放
        CURRENT_JZVD.releaseAllVideos();
    }
}